package com.shinow.capacitor.plusins.speedata.sd60id;

import android.graphics.Bitmap;
import android.util.Base64;

import androidx.appcompat.app.AppCompatActivity;

import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.shinow.capacitor.plusins.speedata.SpeeDataRunTimeException;
import com.speedata.libid2.IDInfor;
import com.speedata.libid2.IDManager;
import com.speedata.libid2.IID2Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Sd60id {
    private static IID2Service iid2Service = IDManager.getInstance();
    private boolean result = false;
    private boolean loop = false;
    private int interval = 1200;

    private static String bitmapToBase64(Bitmap bitmap) {
        String result = null;
        ByteArrayOutputStream baos = null;

        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "data:image/jpeg;base64," + result;
    }

    private static JSObject iDInforToJSObject(IDInfor idInfor) {
        JSObject result = new JSObject();

        result.put("name", idInfor.getName());
        result.put("sex", idInfor.getSex());
        result.put("nation", idInfor.getNation());
        result.put("year", idInfor.getYear());
        result.put("month", idInfor.getMonth());
        result.put("day", idInfor.getDay());
        result.put("address", idInfor.getAddress() != null ? idInfor.getAddress().trim() : idInfor.getAddress());
        result.put("num", idInfor.getNum());
        result.put("QianFa", idInfor.getQianFa() != null ? idInfor.getQianFa().trim() : idInfor.getQianFa());
        result.put("startMonth", idInfor.getStartMonth());
        result.put("startDay", idInfor.getStartDay());
        result.put("endYear", idInfor.getEndYear());
        result.put("endMonth", idInfor.getEndMonth());
        result.put("endDay", idInfor.getEndDay());
        result.put("deadLine", idInfor.getDeadLine());
        result.put("bmps", idInfor.getBmps());
        result.put("image", bitmapToBase64(idInfor.getBmps()));
        result.put("fingerprStringer", idInfor.getFingerprStringer());
        result.put("isSuccess", idInfor.isSuccess());
        result.put("errorMsg", idInfor.getErrorMsg());
        result.put("withFinger", idInfor.isWithFinger());

        return result;
    }

    public void initSd60Id(PluginCall call, AppCompatActivity Activity) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    result = iid2Service.initDev(Activity, (IDInfor) -> {
                        call.resolve(iDInforToJSObject(IDInfor));

                        if (loop) {
                            try {
                                Thread.sleep(interval);
                                getSd60IdInfor();
                            } catch (InterruptedException | SpeeDataRunTimeException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    call.resolve(new JSObject() {{
                        this.put("Initialization", true);
                    }});
                } catch (IOException e) {
                    call.resolve(new JSObject() {{
                        this.put("NativeError", "设备初始化失败");
                    }});
                }
            }
        }).start();
    }

    public void getSd60IdInfor(boolean loop, int interval) throws SpeeDataRunTimeException {
        this.loop = loop;
        this.interval = interval;
        this.getSd60IdInfor();
    }

    private void getSd60IdInfor() throws SpeeDataRunTimeException {
        if (result) {
            this.iid2Service.getIDInforOnce(false);
        } else {
            this.loop = false;
            throw new SpeeDataRunTimeException("设备未初始化");
        }
    }

    public void stopGetSd60IdInfor() {
        this.loop = false;
    }

    public void releaseSd60Id() throws IOException, SpeeDataRunTimeException {
        if (this.result) {
            this.result = false;
            this.iid2Service.releaseDev();
        } else {
            throw new SpeeDataRunTimeException("设备未初始化");
        }
    }
}
